namespace SimpleWebServices.Web.Mvc
{
    using System;
    using System.Globalization;
    using System.Threading;
    using System.Web;
    using System.Web.Mvc;
    using Moq;
    using SimpleWebServices.Web;
    using Xunit;

    public sealed class ContentLanguageAttributeFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<ContentLanguageAttribute>()
                .DerivesFrom<ActionFilterAttribute>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .IsDecoratedWith<AttributeUsageAttribute>()
                .Result);
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new ContentLanguageAttribute());
        }

        [Fact]
        public void op_OnActionExecuted_ActionExecutedContextNull()
        {
            Assert.Throws<ArgumentNullException>(() => new ContentLanguageAttribute().OnActionExecuted(null));
        }

        [Fact]
        public void op_OnActionExecuted_ActionExecutedContextModelNull()
        {
            var mock = new Mock<HttpContextBase>();
            mock.SetupGet<HttpResponseBase>(x => x.Response).Returns(new Mock<HttpResponseBase>().Object).Verifiable();

            new ContentLanguageAttribute().OnActionExecuted(new ActionExecutedContext()
            {
                HttpContext = mock.Object,
                Controller = new TestController<Resource>()
            });

            mock.VerifyAll();
        }

        [Fact]
        public void op_OnActionExecuted_ActionExecutedContextWithModel()
        {
            CultureInfo culture = Thread.CurrentThread.CurrentUICulture;

            try
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr");

                var mock = new Mock<HttpContextBase>();
                mock.Setup(x => x.Response.AppendHeader("Content-Language", "fr")).Verifiable();

                new ContentLanguageAttribute().OnActionExecuted(new ActionExecutedContext()
                {
                    HttpContext = mock.Object,
                    Controller = new TestController<Resource>(new GenericResource())
                });

                mock.VerifyAll();
            }
            finally
            {
                Thread.CurrentThread.CurrentUICulture = culture;
            }
        }
    }
}